package com.laiyizhan.common;

import com.laiyizhan.api.BaseApiResponse;
import com.laiyizhan.common.code.CommonRspCode;
import com.laiyizhan.common.exception.BusinessException;
import com.laiyizhan.common.exception.RepositoryException;
import com.laiyizhan.common.exception.VerifySignatureException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Controller异常统一处理
 */
@ControllerAdvice
public class ControllerExceptionResolver {

    private final Logger log = LoggerFactory.getLogger(ControllerExceptionResolver.class);

    /**
     * 处理数据库异常，打印异常信息，并返回JSON类型
     *
     * @param e
     * @return BaseApiResponse
     */
    @ExceptionHandler({DataAccessException.class, RepositoryException.class})
    @ResponseBody
    public BaseApiResponse handleRepositoryException(Exception e) {
        log.error("数据库异常", e);
        BaseApiResponse response = new BaseApiResponse();
        response.setRespCode(CommonRspCode.DB_ERROR.getCode());
        response.setRespMessage(CommonRspCode.DB_ERROR.getName());
        return response;
    }

    /**
     * 处理参数不合法异常，打印异常信息，并返回JSON类型。
     * 因此代码中判断参数不合法时可直接抛出IllegalArgumentException
     *
     * @param e
     * @return BaseApiResponse
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public BaseApiResponse handleArgumentException(Exception e) {
        log.error("请求参数不合法：{}", e.getMessage());
        BaseApiResponse response = new BaseApiResponse();
        response.setRespCode(CommonRspCode.PARAMBUG.getCode());
        response.setRespMessage(e.getMessage());
        return response;
    }

    /**
     * 签名异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(VerifySignatureException.class)
    @ResponseBody
    public BaseApiResponse handleVerifySignatureException(Exception e) {
        log.error("签名验证失败: {}", e.getMessage());
        BaseApiResponse response = new BaseApiResponse();
        response.setRespCode(CommonRspCode.SIGNATURE_FAIL.getCode());
        response.setRespMessage(e.getMessage());
        return response;
    }

    /**
     * 业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public BaseApiResponse handleBusinessException(Exception e) {
        log.error("业务异常: {}", e.getMessage());
        BaseApiResponse response = new BaseApiResponse();
        response.setRespCode(CommonRspCode.BUSINESS_FAILD.getCode());
        response.setRespMessage(e.getMessage());
        return response;
    }

    /**
     * 处理可抛出的异常，打印异常信息，并返回JSON类型
     *
     * @param e
     * @return BaseApiResponse
     */
    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public BaseApiResponse handleException(Throwable e) {
        log.error("系统内部异常", e);
        BaseApiResponse response = new BaseApiResponse();
        response.setRespCode(CommonRspCode.SYS_ERROR.getCode());
        response.setRespMessage("系统内部异常，请稍后重试");
        return response;
    }

}